﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Neotic.DataAccess.Sql
{
    public class RowCollection : IRowCollection, ICloneable
    {
        internal RowCollection() {}

        protected string _name;
        protected string _schema;
        protected string _aliasName;

        public object Tag { get; set; }

        public virtual bool CanRead
        {
            get { return true; }
        }

        public virtual bool CanWrite
        {
            get { return false; }
        }

        public virtual bool IsSameTable(IRowCollection r)
        { 
            return false;
        }

        public string Name
        {
            get { return _name; }
            internal set { _name = value; }
        }

        public string Schema
        {
            get { return _schema; }
            internal set { _schema = value; }
        }

        public string FullName
        {
            get { 
                if (string.IsNullOrEmpty(this.Schema) && this.Name.StartsWith("#"))
                    return string.Concat("[", this.Name, "]"); 
                else
                    return string.Concat("[", this.Schema, "].[", this.Name, "]"); 
            }
        }

        public string AliasName
        {
            get { return string.IsNullOrWhiteSpace(_aliasName)?this.FullName:_aliasName; }
        }

        public string FullAliasName
        {
            get 
            {
                return (string.IsNullOrWhiteSpace(_aliasName)?this.FullName: string.Concat(this.FullName, " as ", _aliasName)); 
            }
        }

        public IRowCollection As(string aliasName)
        {
            _aliasName = aliasName;
            return this;
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
}
